#!/usr/bin/env python
import rospy
from std_msgs.msg import ByteMultiArray

def callback(data):
    # 将字节数组转换为字节对象
    byte_data = bytes(data.data)
    
    # 尝试将字节解码为字符串
    try:
        # 假设字节数据是UTF-8编码的
        string_data = byte_data.decode('utf-8')
        rospy.loginfo("Received string: %s", string_data)
    except UnicodeDecodeError:
        rospy.logwarn("Could not decode bytes to UTF-8 string")

def listener():
    # 初始化ROS节点
    rospy.init_node('byte_subscriber', anonymous=True)
    
    # 创建一个订阅者，订阅'byte_stream'话题
    rospy.Subscriber('serial_data', ByteMultiArray, callback)
    
    # 保持节点运行，直到关闭
    rospy.spin()

if __name__ == '__main__':
    listener()